package com.czq.controllor;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.czq.common.BaseContext;
import com.czq.common.Result;
import com.czq.pojo.AddressBook;
import com.czq.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/addressBook")
@Transactional
public class AddressBookControllor {
    @Autowired
    private AddressBookService addressBookService;


    //添加地址
    @PostMapping
    public Result<String> addAddressBook(@RequestBody AddressBook addressBook) {

        Long userid = BaseContext.getCurrentId();
        addressBook.setUserId(userid);
        addressBookService.save(addressBook);
        return Result.success("添加地址成功");
    }
    //查询默认地址
    @GetMapping("/default")
    public Result address(){

        LambdaQueryWrapper<AddressBook>  queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);


        return Result.success(one);
    }

    //显示地址
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<AddressBook>();
        queryWrapper.eq(AddressBook::getUserId,userid);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return Result.success(list);
    }

    //设置默认地址
    @PutMapping("/default")
    public Result<String> updatedefault(@RequestBody Map map) {
        Long userid = BaseContext.getCurrentId();
        Object id = map.get("id");
        LambdaUpdateWrapper<AddressBook> q = new LambdaUpdateWrapper<AddressBook>();
        LambdaUpdateWrapper<AddressBook> qq = new LambdaUpdateWrapper<AddressBook>();
        q.set(AddressBook::getIsDefault,0).eq(AddressBook::getUserId,userid);
        addressBookService.update(q);
        qq.set(AddressBook::getIsDefault,1).eq(AddressBook::getId,id);
        addressBookService.update(qq);
        return Result.success("设置默认地址成功");
//        q.set(AddressBook::getIsDefault, 0).eq(AddressBook::getIsDefault, 1);
//        qq.set(AddressBook::getIsDefault, 1).in(AddressBook::getId, id);
//        addressBookService.update(q);
//        addressBookService.update(qq);
//        return Result.success("设置默认地址成功");

    }
    //修改地址回显
    @GetMapping("/{id}")
    public  Result   getAddressBook(@PathVariable Long id ){
        log.info("getAddressBook,{}", id);
        AddressBook address = addressBookService.getById(id);
        return  Result.success(address);
    }
     @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
addressBookService.updateById(addressBook);
return Result.success("修改成功");

     }
}


